/*
   Copyright (c) [Year] [name of copyright holder]
   [Software Name] is licensed under Mulan PSL v2.
   You can use this software according to the terms and conditions of the Mulan PSL v2. 
   You may obtain a copy of Mulan PSL v2 at:
            http://license.coscl.org.cn/MulanPSL2 
   THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.  
   See the Mulan PSL v2 for more details.  
*/

#ifndef JIAJIA_JJSRC_HPP
#define JIAJIA_JJSRC_HPP

#include"jiajiastd.hpp"

namespace jiajia{

    class jjsrc{
    private:
        my::seq<my::unicode_t> mSrc;
    public:
        jjsrc():mSrc(4096){}
        
        // 压入源代码
        void put(my::ubuffer & buf, size_t & text, size_t & size, int & rows, int & cols){
            size = 0;
            rows = buf.rows();
            cols = buf.cols();

            my::unicode_t u;
            while((u = buf.throwUnicode()) != my::unicode::empty){
                mSrc += u;
                ++size;
            }

            if(size == 0){
                // 一般压入的长度为零则证明已经到底了，可以考虑压入 eof 或者 empty
                mSrc += my::unicode::empty;
                text = 0;
            }else
                text = mSrc.size() - size;
        }

        // 获得源代码
        my::ustring get(size_t text, size_t size){
            size_t srcsize = mSrc.size();
            if(text > srcsize){
                text = 0;
                size = 0;
            }else if(srcsize - text < size){
                size = srcsize - text;
            }

            return my::ustring(&mSrc[text], size);
        }

        // 获得指针
        my::unicode_t * getPtr(size_t text){
            size_t size = (size_t)mSrc.size();
            size_t pos = text > size? size-1:text;
            return &mSrc[pos];
        }

        void clear(){
            mSrc.resize(0);
        }

        std::ostream & print(std::ostream & os) const {
            for(long i = 0; i < mSrc.size(); ++i)
                os<<my::utf8::UnicodeToStr(mSrc[i]);
            return os;
        }
    };

    std::ostream & operator<<(std::ostream & os, jjsrc & src){
        return src.print(os);
    }

    static jjsrc src;
    
}
#endif
